# 基于 ubuntu:22.04 构建的 oracle-jdk17
# 参考oracle官网的构建脚本
# https://github.com/oracle/docker-images/blob/main/OracleJava/17/Dockerfile

FROM ubuntu:22.04 as builder

LABEL maintainer="Libo <libo@jiachengnet.com>"
	
ENV JDK_VERSION=17.0.10

# Default to UTF-8 file.encoding
ENV LANG en_US.UTF-8
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
     echo "Asia/Shanghai" >> /etc/timezone

# ENV LANG zh_CN.UTF-8
# JDK17 下载解压

# https://download.oracle.com/java/17/archive/jdk-17.0.10_linux-x64_bin.tar.gz
# https://download.oracle.com/java/17/archive/jdk-17.0.10_linux-x64_bin.tar.gz
# 最新版本地址https://download.oracle.com/java/17/latest

ENV JAVA_URL=https://download.oracle.com/java/17/archive \
	JAVA_HOME=/usr/java/jdk-17

##
# SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# sed -i 's/ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    chmod 777 /tmp && \
	apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates curl

RUN set -eux; \
	ARCH="$(uname -m)" && \
	# Java uses just x64 in the name of the tarball
    if [ "$ARCH" = "x86_64" ]; \
        then ARCH="x64"; \
    fi && \
    JAVA_PKG="$JAVA_URL"/jdk-${JDK_VERSION}_linux-"${ARCH}"_bin.tar.gz ; \
	JAVA_SHA256="$(curl "$JAVA_PKG".sha256)" ; \
	curl --output /tmp/jdk.tgz "$JAVA_PKG" && \
	echo "$JAVA_SHA256" */tmp/jdk.tgz | sha256sum -c; \
	mkdir -p "$JAVA_HOME"; \
	tar --extract --file /tmp/jdk.tgz --directory "$JAVA_HOME" --strip-components 1

## Get a fresh version of ubuntu for the final image	
FROM ubuntu:22.04

RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
	chmod 777 /tmp && \
    apt-get update && \
    apt-get install -y --no-install-recommends tzdata vim iputils-ping && \
    ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Default to UTF-8 file.encoding
ENV LANG en_US.UTF-8
ENV JAVA_HOME=/usr/java/jdk-17
ENV PATH $JAVA_HOME/bin:$PATH

# Copy the uncompressed Java Runtime from the builder image
COPY --from=builder $JAVA_HOME $JAVA_HOME

RUN	ln -sfT "$JAVA_HOME" /usr/java/default; \
	ln -sfT "$JAVA_HOME" /usr/java/latest; \
	for bin in "$JAVA_HOME/bin/"*; do \
		base="$(basename "$bin")"; \
		[ ! -e "/usr/bin/$base" ]; \
		update-alternatives --install "/usr/bin/$base" "$base" "$bin" 20000; \
	done;

